package com.yazdee.productdetail;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ProductDetailHandler extends DefaultHandler{

	private boolean in_imageURLtag = false;
	private boolean in_nametag = false;
    private boolean in_pricetag = false;
    private boolean in_productURLtag = false;
    private List resultRow=new ArrayList();
    private ProductObject product;
    
    public ProductObject getProductDetail(){
    	return this.product;
    }
    
    @Override
    public void startDocument() throws SAXException {
    	product=new ProductObject();
    }

    @Override
    public void endDocument() throws SAXException {
    	
    }
    
    @Override
    public void startElement(String namespaceURI, String localName,
              String qName, Attributes atts) throws SAXException {
    	if (localName.equals("name")) {
            this.in_nametag = true;
    	}else if (localName.equals("price")) {
              this.in_pricetag = true;
         }else if (localName.equals("imageURL")) {
              this.in_imageURLtag = true;
         }else if (localName.equals("productURL")) {
              this.in_productURLtag = true;
         }
    }
    
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
              throws SAXException {
    	if (localName.equals("name")) {
            this.in_nametag = true;
    	}else if (localName.equals("price")) {
              this.in_pricetag = true;
         }else if (localName.equals("imageURL")) {
              this.in_imageURLtag = true;
         }else if (localName.equals("productURL")) {
              this.in_productURLtag = true;
         }
    }
    
   @Override
   public void characters(char ch[], int start, int length) {
	   if (this.in_nametag) {
            product.setName(new String(ch,start,length));
   		}else if (this.in_pricetag) {
              product.setPrice(new String(ch,start,length));
        }else if (this.in_imageURLtag) {
              product.setImageURL(new String(ch,start,length));
        }else if (this.in_productURLtag) {
              product.setProductURL(new String(ch,start,length));
        }
	   } 
}
